/* <pre>
 * Copyright (c) The Loose Coupling Team.
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *</pre>
 */

package org.lc.eic.synchronizer.replier;

import org.lc.eic.api.requestreply.async.Replier;
import org.lc.eic.api.requestreply.sync.RequestDispatcher;
import org.lc.eic.core.pipesandfilters.Pipe;
import org.lc.eic.core.pipesandfilters.exception.ProcessingException;



/**
 * Bridges from asynchronous to a synchronous request on the replier.
 *	@author <a href="mailto:p.lycke@googlemail.com">PaLy</a>
 *
 * 
 */
public class ReplierSynchronizer<I, O>  implements Replier<I, O>{
	
	
	private RequestDispatcher<I, O> requestDispatcher;
		
	

	private Pipe<O> replyPipe;

	/**
	 * @see org.lc.eic.api.requestreply.async.RequestDispatcher#getRequestPipe()
	 */
	public Pipe<I> getRequestPipe() {
		return createRequestPipe();
	}
	
	private Pipe<I> createRequestPipe(){
		return new Pipe<I>() {
			public void process(I request) throws ProcessingException{
				O reply =requestDispatcher.request(request);
				replyPipe.process(reply);
				
			}
		};
	}

	/**
	 * @see org.lc.eic.api.requestreply.async.RequestDispatcher#setReplyPipe(org.lc.eic.core.pipesandfilters.Pipe)
	 */
	public void setReplyPipe(Pipe<O> replyPipe) {
		this.replyPipe=replyPipe;
		
	}
	
	public void setRequestDispatcher(RequestDispatcher<I, O> requestDispatcher) {
		this.requestDispatcher = requestDispatcher;
	}
	
	
	

}
